package com.ronghui.cash.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class FileUtil {
	/**
	 * 获取唯一字符串 用于文件上件的随机名
	 */
	public static String getTimeRand(String filename) {
		StringBuffer buf = new StringBuffer();

		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");

		String timestamp = sdf.format(new Date());

		buf.append(timestamp);// 加上日期
		Random random = new Random();
		for (int i = 0; i < 7; i++) {
			buf.append(random.nextInt(10));// 取7个随机数追加到StringBuffer
		}

		int i = filename.lastIndexOf(".");// 返回最后一个点的位置
		String extension = filename.substring(i + 1);// 取出扩展名

		buf.append("." + extension);// 加上扩展名
		return buf.toString();

	}

	/**
	 * 保存文件
	 * @param file
	 * @param path
	 * @param delPath
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public static String saveFile(MultipartFile file,String path,String delPath) throws IllegalStateException, IOException {
		String fileName = file.getOriginalFilename();// 文件原名称
		fileName = getTimeRand(fileName);
		path = path + fileName;
		file.transferTo(new File(path));
		if(StringUtil.isNotEmpty(delPath)){
			deleteFile(delPath);
		}
		return fileName;
	}

	/**
	 * 删除文件
	 * @param path
	 */
	public static void deleteFile(String path) {
		File file = new File(path);
		if(file.exists()){
			boolean d = file.delete();
			if(d){
				System.out.print("删除成功！");
			}else{
				System.out.print("删除失败！");
			}
		}
	}

	public static boolean existsFile(String path){
		File oldfile = new File(path);
		return oldfile.exists();
	}

	/**
	* 复制单个文件
	* @param oldPath String 原文件路径 如：c:/fqf.txt
	* @param newPath String 复制后路径 如：f:/fqf.txt
	* @return boolean
	*/
	public static boolean copyFile(String oldPath, String newPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { //文件存在时
				InputStream inStream = new FileInputStream(oldPath); //读入原文件
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				int length;
				while ( (byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; //字节数 文件大小
//					System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}else{
				return false;
			}
		}
		catch (Exception e) {
			System.out.println("复制单个文件操作出错");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 复制整个文件夹内容
	 * @param oldPath String 原文件路径 如：c:/fqf
	 * @param newPath String 复制后路径 如：f:/fqf/ff
	 * @return boolean
	 */
	public static void copyFolder(String oldPath, String newPath) {

		try {
			(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
			File a=new File(oldPath);
			String[] file=a.list();
			File temp=null;
			for (int i = 0; i < file.length; i++) {
				if(oldPath.endsWith(File.separator)){
					temp=new File(oldPath+file[i]);
				}
				else{
					temp=new File(oldPath+File.separator+file[i]);
				}

				if(temp.isFile()){
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath + "/" +
							(temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ( (len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if(temp.isDirectory()){//如果是子文件夹
					copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
				}
			}
		}
		catch (Exception e) {
			System.out.println("复制整个文件夹内容操作出错");
			e.printStackTrace();

		}

	}

}
